﻿using Equipment;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DAL;
using Model;
using System.IO.Ports;
using Common;

namespace OliveAirLeakTest
{
    public partial class SettingFrm : Form

    {
        List<Product> myProducts = new List<Product>();
        BindingSource myBindingSource = new BindingSource();
        public SettingFrm()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (cmbScanCom.Text == "" || cmbScanCom.Text ==null)
            {
                MessageBox.Show("串口号不能为空");
                return;
            }
            else
            {
                ConfigHelper.WriteIni("SystemSettings", "ScanCom", cmbScanCom.Text);
            }
            if(cmbATEQCom.Text =="" || cmbATEQCom.Text == null)
            {
                MessageBox.Show("串口号不能为空");
                return;
            }
            else
            {
                ConfigHelper.WriteIni("SystemSettings", "ATEQCom", cmbATEQCom.Text);
            }

            if(tbScanAddr.Text == "")
            {
                MessageBox.Show("扫码完成寄存器地址不能为空");
                return;
            }
            else
            {
                ConfigHelper.WriteIni("SystemSettings", "PLCAddrScanCompeleted",tbScanAddr.Text);
            }
            if(tbATEQAddr.Text == "")
            {
                MessageBox.Show("PLC测试中寄存器地址不能为空");
                return;
            }
            else
            {
                ConfigHelper.WriteIni("SystemSettings", "PLCAddrTesting", tbATEQAddr.Text);
            }

            ConfigHelper.WriteIni("MESSettings", "UrlGet", tbUrlGet.Text);
            ConfigHelper.WriteIni("MESSettings", "UrlLoad", tbUrlLoad.Text);
            ConfigHelper.WriteIni("MESSettings", "UrlReport", tbUrlReport.Text);
            ConfigHelper.WriteIni("MESSettings", "UrlUpdata", tbUrlUpdata.Text);

            MessageBox.Show("系统配置保存成功！");
        }

        private void SettingFrm_Load(object sender, EventArgs e)
        {
            SqlSugarHelper sqlSugarHelper = new SqlSugarHelper();
            myProducts = sqlSugarHelper.QueryAll<Product>();
            myBindingSource.DataSource = myProducts;
            dataGridView1.DataSource = myBindingSource;
            bindingNavigator1.BindingSource = myBindingSource;

            tbProductName.DataBindings.Add("TEXT", myBindingSource, "Name");
            tbCharacterCode.DataBindings.Add("TEXT", myBindingSource, "CharacterCode");
            tbCodeLength.DataBindings.Add("TEXT", myBindingSource, "Length");
            tbCClength.DataBindings.Add("TEXT", myBindingSource, "CCodeLength");
            tbCCodeLength.DataBindings.Add("TEXT", myBindingSource, "CCodeLocation");
            cbIsTask.DataBindings.Add("CHECKED", myBindingSource, "IsTask");
            cbIsLoad.DataBindings.Add("CHECKED", myBindingSource, "IsLoad");
            cbIsReport.DataBindings.Add("CHECKED", myBindingSource, "IsReport");
            cbIsUpdata.DataBindings.Add("CHECKED", myBindingSource, "IsUpdata");
            tbEquipmentCode.DataBindings.Add("TEXT", myBindingSource, "EquipmentCode");
            tbUserId.DataBindings.Add("TEXT", myBindingSource, "UserId");
            tbMaterialCode.DataBindings.Add("TEXT", myBindingSource, "MaterialCode");

            dataGridView1.Columns[0].Width = 50;
            dataGridView1.Columns[1].Width = 150;
            dataGridView1.Columns[2].Width = 300;
            dataGridView1.Columns[3].Width = 85;
            dataGridView1.Columns[4].Width = 85;
            dataGridView1.Columns[5].Width = 85;

            cmbScanCom.Text = ConfigHelper.ReadIni("SystemSettings", "ScanCom", "COM1", 255);
            cmbATEQCom.Text = ConfigHelper.ReadIni("SystemSettings", "ATEQCom", "COM2", 255);
            tbATEQAddr.Text = ConfigHelper.ReadIni("SystemSettings", "PLCAddrTesting", "M500", 255);
            tbScanAddr.Text = ConfigHelper.ReadIni("SystemSettings", "PLCAddrScanCompeleted", "M100", 255);
            tbUrlGet.Text = ConfigHelper.ReadIni("MESSettings", "UrlGet", "http://mes.oliveair.com/api/equipmentTask/getTaskByEquipmentCode",255);
            tbUrlLoad.Text = ConfigHelper.ReadIni("MESSettings", "UrlLoad", "http://mes.oliveair.com/api/equipmentTask/loadTask",255);
            tbUrlReport.Text = ConfigHelper.ReadIni("MESSettings", "UrlReport", "http://mes.oliveair.com/api/airLeakTest/report",255);
            tbUrlUpdata.Text = ConfigHelper.ReadIni("MESSettings", "UrlUpdata", "http://mes.oliveair.com/api/equipmentTask/updateTaskStatus",255);
        }

        private void btnSaveProduct_Click(object sender, EventArgs e)
        {
            SqlSugarHelper sqlSugarHelper = new SqlSugarHelper();
            if (sqlSugarHelper.UpdateAll<Product>(myProducts))
            {
                MessageBox.Show("当前产品修改保存成功");
            }
        }

        private void bindingNavigatorAddNewItem_Click(object sender, EventArgs e)
        {
            Product newProduct = new Product();
            SqlSugarHelper sqlSugarHelper = new SqlSugarHelper();
            if (sqlSugarHelper.Insert<Product>(newProduct))
            {
                myProducts = sqlSugarHelper.QueryAll<Product>();
                myBindingSource.DataSource = myProducts;
                dataGridView1.DataSource = myBindingSource;
                bindingNavigator1.BindingSource = myBindingSource; ;
            }
        }

        private void bindingNavigatorDeleteItem_Click(object sender, EventArgs e)
        {
            SqlSugarHelper sqlSugarHelper = new SqlSugarHelper();
            if (MessageBox.Show($"请确认是否要删除选中的产品{myProducts[dataGridView1.CurrentCellAddress.Y].Name}","警告",MessageBoxButtons.YesNo,MessageBoxIcon.Warning) == DialogResult.Yes)
            {
                if (sqlSugarHelper.Delete<Product>(myProducts[dataGridView1.CurrentCellAddress.Y].Id))
                {
                    MessageBox.Show("产品删除成功！");
                }
                else
                {
                    MessageBox.Show("产品删除失败！");
                }
                myProducts = sqlSugarHelper.QueryAll<Product>();
                myBindingSource.DataSource = myProducts;

                dataGridView1.DataSource = myBindingSource;
                bindingNavigator1.BindingSource = myBindingSource;
                
            }
        }

        private void cmbATEQCom_DropDown(object sender, EventArgs e)
        {
            cmbATEQCom.Items.Clear();
            string[] names = SerialPort.GetPortNames();
            foreach (string name in names)
            {
                cmbATEQCom.Items.Add(name);
            }
        }

        private void cmbScanCom_DropDown(object sender, EventArgs e)
        {
            cmbScanCom.Items.Clear();
            string[] names = SerialPort.GetPortNames();
            foreach(string name in names)
            {
                cmbScanCom.Items.Add(name);
            }
        }
    }
}
